/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.base.service.supplier;

import com.jeesite.common.entity.Page;
import com.jeesite.common.service.CrudService;
import com.jeesite.modules.base.dao.supplier.SupplierBusinessInfoDao;
import com.jeesite.modules.base.dao.supplier.SupplierContactInfoDao;
import com.jeesite.modules.base.dao.supplier.SupplierCustomBrokerDao;
import com.jeesite.modules.base.dao.supplier.SupplierDao;
import com.jeesite.modules.base.entity.supplier.Supplier;
import com.jeesite.modules.base.entity.supplier.SupplierBusinessInfo;
import com.jeesite.modules.base.entity.supplier.SupplierContactInfo;
import com.jeesite.modules.base.entity.supplier.SupplierCustomBroker;
import com.jeesite.modules.custom.service.CodeRuleService;
import com.jeesite.modules.file.utils.FileUploadUtils;
import com.jeesite.modules.sys.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 供应商Service
 * @author LV
 * @version 2021-03-01
 */
@Service
@Transactional(readOnly=true)
public class SupplierService extends CrudService<SupplierDao, Supplier> {
	
	@Autowired
	private SupplierBusinessInfoDao supplierBusinessInfoDao;
    @Autowired
    private CodeRuleService codeRuleService;
//	@Autowired
//	private SupplierBusinessTypeDao supplierBusinessTypeDao;
	
	@Autowired
	private SupplierContactInfoDao supplierContactInfoDao;
	
	@Autowired
	private SupplierCustomBrokerDao supplierCustomBrokerDao;
	
	/**
	 * 获取单条数据
	 * @param supplier
	 * @return
	 */
	@Override
	public Supplier get(Supplier supplier) {
		Supplier entity = super.get(supplier);
		if (entity != null){
			SupplierBusinessInfo supplierBusinessInfo = new SupplierBusinessInfo(entity);
			supplierBusinessInfo.setStatus(SupplierBusinessInfo.STATUS_NORMAL);
			entity.setSupplierBusinessInfoList(supplierBusinessInfoDao.findList(supplierBusinessInfo));
//			SupplierBusinessType supplierBusinessType = new SupplierBusinessType(entity);
//			supplierBusinessType.setStatus(SupplierBusinessType.STATUS_NORMAL);
//			entity.setSupplierBusinessTypeList(supplierBusinessTypeDao.findList(supplierBusinessType));
			SupplierContactInfo supplierContactInfo = new SupplierContactInfo(entity);
			supplierContactInfo.setStatus(SupplierContactInfo.STATUS_NORMAL);
			entity.setSupplierContactInfoList(supplierContactInfoDao.findList(supplierContactInfo));
			SupplierCustomBroker supplierCustomBroker = new SupplierCustomBroker(entity);
			supplierCustomBroker.setStatus(SupplierCustomBroker.STATUS_NORMAL);
			entity.setSupplierCustomBrokerList(supplierCustomBrokerDao.findList(supplierCustomBroker));
		}
		return entity;
	}
	
	/**
	 * 查询分页数据
	 * @param supplier 查询条件
	 * @param supplier.page 分页对象
	 * @return
	 */
	@Override
	public Page<Supplier> findPage(Supplier supplier) {
		return super.findPage(supplier);
	}
	
	/**
	 * 保存数据（插入或更新）
	 * @param supplier
	 */
	@Override
	@Transactional(readOnly=false)
	public void save(Supplier supplier) {
        if (supplier.getIsNewRecord()) {
            supplier.setSupplierId(codeRuleService.getCode("vendor"));
			supplier.setCreateBy(UserUtils.getUser().getUserCode());
			supplier.setUpdateBy(UserUtils.getUser().getUserCode());
        }
        super.save(supplier);
        if (supplier.getIsNewRecord()) {
            codeRuleService.updateCurrentNumber("vendor");
        }else {
			supplier.setUpdateBy(UserUtils.getUser().getUserCode());
		}
		// 保存上传附件
		FileUploadUtils.saveFileUpload(supplier.getId(), "supplier_file");
		// 保存 Supplier子表
        int index = 0;
		for (SupplierBusinessInfo supplierBusinessInfo : supplier.getSupplierBusinessInfoList()){
			if (!SupplierBusinessInfo.STATUS_DELETE.equals(supplierBusinessInfo.getStatus())){
				supplierBusinessInfo.setSupplierId(supplier);
				if (supplierBusinessInfo.getIsNewRecord()){
					supplierBusinessInfoDao.insert(supplierBusinessInfo);
				}else{
					supplierBusinessInfoDao.update(supplierBusinessInfo);
				}
			}else{
				supplierBusinessInfoDao.delete(supplierBusinessInfo);
			}
            // 保存上传附件
            FileUploadUtils.saveFileUpload(supplierBusinessInfo.getId(),
                    "supplierBusinessInfoList["+index+"].supplierBusinessInfoData_file");
            index++;

		}
		// 保存 Supplier子表
		/*for (SupplierBusinessType supplierBusinessType : supplier.getSupplierBusinessTypeList()){
			if (!SupplierBusinessType.STATUS_DELETE.equals(supplierBusinessType.getStatus())){
				supplierBusinessType.setSupplierId(supplier);
				if (supplierBusinessType.getIsNewRecord()){
					supplierBusinessTypeDao.insert(supplierBusinessType);
				}else{
					supplierBusinessTypeDao.update(supplierBusinessType);
				}
			}else{
				supplierBusinessTypeDao.delete(supplierBusinessType);
			}
		}*/
		// 保存 Supplier子表
		for (SupplierContactInfo supplierContactInfo : supplier.getSupplierContactInfoList()){
			if (!SupplierContactInfo.STATUS_DELETE.equals(supplierContactInfo.getStatus())){
				supplierContactInfo.setSupplierId(supplier);
				if (supplierContactInfo.getIsNewRecord()){
					supplierContactInfoDao.insert(supplierContactInfo);
				}else{
					supplierContactInfoDao.update(supplierContactInfo);
				}
			}else{
				supplierContactInfoDao.delete(supplierContactInfo);
			}
		}
		// 保存 Supplier子表
		for (SupplierCustomBroker supplierCustomBroker : supplier.getSupplierCustomBrokerList()){
			if (!SupplierCustomBroker.STATUS_DELETE.equals(supplierCustomBroker.getStatus())){
				supplierCustomBroker.setSupplierId(supplier);
				if (supplierCustomBroker.getIsNewRecord()){
					supplierCustomBrokerDao.insert(supplierCustomBroker);
				}else{
					supplierCustomBrokerDao.update(supplierCustomBroker);
				}
			}else{
				supplierCustomBrokerDao.delete(supplierCustomBroker);
			}
		}
	}
	
	/**
	 * 更新状态
	 * @param supplier
	 */
	@Override
	@Transactional(readOnly=false)
	public void updateStatus(Supplier supplier) {
		super.updateStatus(supplier);
	}
	
	/**
	 * 删除数据
	 * @param supplier
	 */
	@Override
	@Transactional(readOnly=false)
	public void delete(Supplier supplier) {
		super.delete(supplier);
		SupplierBusinessInfo supplierBusinessInfo = new SupplierBusinessInfo();
		supplierBusinessInfo.setSupplierId(supplier);
		supplierBusinessInfoDao.deleteByEntity(supplierBusinessInfo);
//		SupplierBusinessType supplierBusinessType = new SupplierBusinessType();
//		supplierBusinessType.setSupplierId(supplier);
//		supplierBusinessTypeDao.deleteByEntity(supplierBusinessType);
		SupplierContactInfo supplierContactInfo = new SupplierContactInfo();
		supplierContactInfo.setSupplierId(supplier);
		supplierContactInfoDao.deleteByEntity(supplierContactInfo);
		SupplierCustomBroker supplierCustomBroker = new SupplierCustomBroker();
		supplierCustomBroker.setSupplierId(supplier);
		supplierCustomBrokerDao.deleteByEntity(supplierCustomBroker);
	}


    public Supplier getByAbbreviation(Supplier supplier) {
	    Supplier where = new Supplier();
	    where.setSupplierNameAbbreviation(supplier.getSupplierNameAbbreviation());
	    return dao.getByEntity(where);
    }
}